var $;
var form;
var layer;
var upload;
$(function() {
    layui.use(['form', 'layer','upload','element'], function () {
        //获取jquery的$
        $=layui.jquery;
        //获取form表单对象，封装方法获取form表单的
        form=layui.form;
        //获取layer对象
        layer=layui.layer;
        upload=layui.upload;

        //获取登录用户的信息
        $.ajax({
            url:"/login/selUserContent",
            type:"post",
            dataType:"json",
            data:{},
            success:function(data){
                console.log(data.obj.account);
                $("#userName").val(data.obj.account);
                $("#email").val(data.obj.email);
                $("#userId").val(data.obj.userId);
                $("#userPwd").val(data.obj.password);
            }
        });

        //编辑按钮
        $("#butId").on("click",function(){
            $("#pudId").css("display","block");
            $("#butId").css("display","none");
            $("#userName").removeAttr("readonly");
            $("#email").removeAttr("readonly");
            $("#ppwd").removeAttr("readonly");
            $("#npwd").removeAttr("readonly");
            $("#npwd2").removeAttr("readonly");
        });

        //点击上传图片
        $("#updimga").on("click",function(){
            console.log("更新图片");
            $("#imgbut").click();
        });

        //图片上传
            var uploadInst = upload.render({
                elem: '#imgbut'
                , url: '/login/updimg' //改成您自己的上传接口
                , before: function (obj) {
                    //预读本地文件示例
                    obj.preview(function (index, file, result) {
                        $('#layui_i1').css("display","none");
                        $('#imgId').attr('src', result); //图片链接（base64）
                        $("#imgurlId").val(result);
                    });
                }
                , done: function (res) {
                    var url=$("#imgurlId").val();
                    console.log(url);
                    layer.msg('上传完毕', {icon: 1});
                    //上传成功的一些操作
                }
                , error: function () {
                    //失败
                    layer.msg('上传失败', {icon: 2});
                }
            });

        //提交按钮
        $("#pudId").on("click",function(){
            //判断旧密码是否正确&新密码是否一致
            var ppwd=$('#ppwd').val();
            var userPwd=$('#userPwd').val();
            var pwd1=$('#npwd').val();
            var pwd2=$('#npwd2').val();
            console.log("旧密码:"+ppwd+"**"+userPwd);console.log("xin密码:"+pwd1+"**"+pwd2);
            console.log(ppwd);
            if(ppwd==userPwd && ppwd!==""){
                if(pwd1==pwd2 && pwd1!=="" && pwd2!==""){
                    var useremail=$("#email").val();
                    console.log(useremail);
                    //发送邮件
                    sendmail(useremail);

                    //验证弹窗
                    layer.open({
                        type: 1,
                        title: "验证码验证",
                        area: ['450px', '450px'],
                        shade: 0.4,
                        offset: 't',
                        content: $('#layerId'),
                        success: function (layero, index) {
                            //选择元素,并设置值
                            $("#userEmailId").val(useremail);
                        },
                        btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            var eamil1=$("#emailId").val();
                            console.log(eamil1);
                            $("#mailId2").val(eamil1);

                            //判断验证码是否正确
                            var mail=$("#mailId").val();
                            var mail2=$("#mailId2").val();
                            if(mail==mail2){
                                layer.msg('验证成功');
                                //updform();
                            }else{
                                layer.alert('验证码错误！', {icon: 2});
                            }

                            //关闭弹窗
                            layer.close(index);
                        }
                        ,btn2: function(index, layero){
                            //关闭弹窗
                            layer.close(index);
                        }
                    });
                }else{
                    layer.alert('新密码不一致！', {icon: 2});
                }
            }else{
                layer.alert('旧密码错误！', {icon: 2});
            }
        });
        
        
        //发送邮件验证码
        function sendmail() {
            $.ajax({
                url:"/login/selmail",
                type:"post",
                dataType:"json",
                data:{},
                success:function(data){
                    console.log(data.obj);
                    $("#mailId").val(data.obj);
                }
            });
        }


        //提交到后台
        function updform(){
            var formcont=new FormData(document.getElementById("accountForm"));
            $.ajax({
                url:"/login/updUser",
                type:"post",
                dataType:"json",
                data:formcont,
                contentType:false,//不转换为其他格式，是JS的form表单对象
                processData:false,
                success:function(data){
                    //console.log(data);
                    if(data.code>-1){
                        layer.alert('提交成功！', {icon: 1}, function(index){
                            //关闭窗口并刷新页面
                            layer.close(index);
                            location.reload(true);
                        });
                    }else{
                        layer.alert('提交失败！', {icon: 2});
                    }
                    //添加完成后赋值id,name。判断是否走下一步
                    //$("#newsbId").val(data.eqid);
                    //$("#newsb_name").val(data.eqname);
                }
            });
        }
    });

});